package com.example.brazil_project.function;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.brazil_project.ArriveMsgActivity;
import com.example.brazil_project.DucumentActivity;
import com.example.brazil_project.DucumentDetailActivity;
import com.example.brazil_project.PopularizeMsgActivity;
import com.example.brazil_project.PromotionMsgActivity;
import com.example.brazil_project.SysMsgContentActivity;
import com.example.brazil_project.SysMsgInfoActivity;
import com.example.brazil_project.parameter.Parameter;
import com.example.brazil_project.util.HttpUtil;
import com.example.brazil_project.util.JSFunction;
import com.example.brazil_project.util.ParameterUtil;
import com.example.brazil_project.util.SQLiteDatabaseUtil;
import com.example.brazil_project.util.jsonUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.webkit.WebView;

public class MsgFunction {
	Activity mContxt;  
	private String username;
	private WebView webview;
	public static String msgjson =null;
	 public MsgFunction(Activity mContxt,Object obje) {  
	        this.mContxt = mContxt;  
	        this.webview = (WebView) obje;
	        Public_method Public_method = new Public_method(mContxt);
	        username = Public_method.getLoginUserName();
	  }  
	/**
	 * 跳转信息详情页
	 * @param type
	 * @param json
	 */
	 public void gotoMsg(String type,String json){
		 Intent intent = new Intent(mContxt, ArriveMsgActivity.class);
		 
		 if(type.equals("ArriveMsg")){
			  intent = new Intent(mContxt, ArriveMsgActivity.class);
		 }
		 if(type.equals("Sysmsg")){
			  intent = new Intent(mContxt, SysMsgInfoActivity.class);
		 }
		 if(type.equals("PopMsg")){
			  intent = new Intent(mContxt, PopularizeMsgActivity.class);
		 }
		 if(type.equals("ProMsg")){
			  intent = new Intent(mContxt, PromotionMsgActivity.class);
		 }
		 msgjson = json;
		 mContxt.startActivity(intent);
	 }
	 
	 // 系统信息汇总
	
	 public String sysmsgGather(){
		 List list = new ArrayList();
		 Map map = new HashMap();
		 int i = 0;
		 //获取系统信息
		 String selectsql = "SELECT * FROM SystemInform_table where type in(1,2) and username = '"+username+"' order by id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 list = getListbyC(c);
//		String json = jsonUtil.getJsonbymap(map);	
//		
		String listjson =  jsonUtil.getJsonbylist(list);
		return listjson;
		 
		 
	 }
	 
	 //根据登录人获取到货信息
	 public String getArriveMsgby(){
		 List list = new ArrayList();
		 String selectsql = "SELECT * FROM ArriveInform_table where type in(1,2) and username = '"+username+"' order by id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 list = SQLiteDatabaseUtil.getListbyCursor(c);
		 String listjson =  jsonUtil.getJsonbylist(list);
		 return listjson;
		 
		 
	 }
	 
	 //根据登录人获取推广信息
	 public String getPopularize(){
		 List list = new ArrayList();
		 String selectsql = "SELECT * FROM ArriveInform_table where type in(3) and username = '"+username+"' order by id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 list = SQLiteDatabaseUtil.getListbyCursor(c);
		 String listjson =  jsonUtil.getJsonbylist(list);
		 return listjson;
		 
		 
	 }
	 
	//根据登录人获取促销信息
		 public String getPromotion(){
			 List list = new ArrayList();
			 String selectsql = "SELECT * FROM ArriveInform_table where type in(4) and username = '"+username+"' order by id desc";
			 Cursor c = SQLiteDatabaseUtil.select(selectsql);
			 list = SQLiteDatabaseUtil.getListbyCursor(c);
			 String listjson =  jsonUtil.getJsonbylist(list);
			 return listjson;
			 
			 
		 }
	 
	 
	 //根据json跳转到信息内容的页面
	 public void chackMSGCentent(String json){
		 
		 Intent intent = new Intent(mContxt, SysMsgContentActivity.class);
		 msgjson = json;
		 mContxt.startActivity(intent);
		 
	 }
	
	 //返回信息json
	 public String getmsgjson(){
		return msgjson;
		 
		 
	 }
	 
	 //根据用户名和ID查询信息
	 public String getmsgByIDandUsername(String id){
		 String selectsql = "SELECT * FROM SystemInform_table where  id="+id+" and username = '"+username+"' order by id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 //更新为已经读
		 selectsql = "update SystemInform_table set isread = '1' where  id="+id+" and username = '"+username+"'";
		 SQLiteDatabaseUtil.insert(selectsql);
		 
		 List list = getListbyC(c);
		 String listjson =  jsonUtil.getJsonbylist(list);
		 
		return listjson;
		 
		 
		 
	 }
	 
	 
	 //获取到我的到空运信息
	 public String getAir(){
		 List list = new ArrayList();
		 String selectsql = "SELECT * FROM ArriveInform_table where type in(1) and username = '"+username+"' order by id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 list = SQLiteDatabaseUtil.getListbyCursor(c);
		 String listjson =  jsonUtil.getJsonbylist(list);
		 return listjson;
		 
	 }
	 
	 
	 //获取到我的到新柜信息
	 public String getNew(){
		 List list = new ArrayList();
		 String selectsql = "SELECT * FROM ArriveInform_table where type in(2) and username = '"+username+"' order by id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 list = SQLiteDatabaseUtil.getListbyCursor(c);
		 String listjson =  jsonUtil.getJsonbylist(list);
		 return listjson;
		 
	 }
	 //根据推广信息获取产品
	 public String getProductByMsgId(String id){
		 List list = new ArrayList();
		 String selectsql = "SELECT * FROM ArriveProduct_table where msg_id = "+id+" order by msg_id desc";
		 Cursor c = SQLiteDatabaseUtil.select(selectsql);
		 list = SQLiteDatabaseUtil.getListbyCursor(c);
		 String listjson =  jsonUtil.getJsonbylist(list);
		 return listjson;
		
		 
	 }
	 
	 
	 //根据查询结果放回list
	private List getListbyC(Cursor c){
			List list = new ArrayList();
		
		 int i = 0;
		 while (c.moveToNext()) { 
			 Map map = new HashMap();
			 i=i+1;
			 map.put("id", c.getString(c.getColumnIndex("id")));
			 map.put("count", i);
			 map.put("title", c.getString(c.getColumnIndex("title")));
			 map.put("content", c.getString(c.getColumnIndex("content")));
			 map.put("CreateDate", c.getString(c.getColumnIndex("CreateDate")));
			 map.put("type", c.getString(c.getColumnIndex("type")));
			 map.put("username", c.getString(c.getColumnIndex("username")));
			 map.put("reply", c.getString(c.getColumnIndex("reply")));
			 map.put("isread", c.getString(c.getColumnIndex("isread")));
			 list.add(map);
		 }
		c.close();
		return list;
		
	}
	/**
	 * 添加一个已经反馈
	 * @param data
	 */
	public void submitMSG(String data){
		String url = Parameter.getServerPath()+"APP/msg/addOpinion.nut";
		 Map map = ParameterUtil.getParameter(data);
		String msg = HttpUtil.doPost(url,map);
		if(msg.equals("1")){
			msg= "已经成功提交";
		}
		webview.loadUrl(JSFunction.getAlertMSGFunction(msg));
	}
	/**
	 * 到达信息添加
	 * @param data
	 */
	public void submitArrMSG(String data){
		Public_method Public_method = new Public_method(mContxt);
		String url = Parameter.getServerPath()+"APP/msg/addArriveMsg.nut";
		 Map map = ParameterUtil.getParameter(data);
		 map.put("username", Public_method.getLoginUserName());
		String msg = HttpUtil.doPost(url,map);
		if(msg.equals("1")){
			msg= "已经成功提交";
		}
		
		webview.reload();
		webview.loadUrl(JSFunction.getAlertMSGFunction(msg));
	}
}
